package ch.zbw.timeline.service;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Security class to generate the MD5 hash code form the passwort
 * 
 * @author Andreas Kaderli
 * @version 24.12.2011
 *
 */
public class ZbwSecurityService implements SecurityService {

	/*
	 * (non-Javadoc)
	 * @see ch.zbw.timeline.service.SecurityService#generateMd5(java.lang.String)
	 */
	@Override
	public String generateMd5(String password) {
		String res = "";
		try {
			MessageDigest algorithm = MessageDigest.getInstance("MD5");
			algorithm.reset();
			algorithm.update(password.getBytes());
			byte[] md5 = algorithm.digest();
			String tmp = "";
			for (int i = 0; i < md5.length; i++) {
				tmp = (Integer.toHexString(0xFF & md5[i]));
				if (tmp.length() == 1) {
					res += "0" + tmp;
				} else {
					res += tmp;
				}
			}
		} catch (NoSuchAlgorithmException ex) {
		}
		return res;
	}

}
